package ah.dom;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class TestDom4j {

	/**
	 * @author dingdahai Mar 26, 2010 1:35:13 PM
	 * @param args
	 */
	public static void main(String[] args) {
		new TestDom4j().test();
	}

	public void test() {
		long lasting = System.currentTimeMillis();
		try {
			File f = new File("src/dom/NewFile.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(f);
			Element root = doc.getRootElement();
			Element foo = null;
			
			/** 读命名空间中值 */
			Map<String,String> nsMap = new HashMap<String,String>();  
	         nsMap.put("ns","http://www.springframework.org/schema/beans");// 加入命名空间  
	          XPath xsub = doc.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径,取属性的话,//ns:beans/ns:bean/@id
	         xsub.setNamespaceURIs(nsMap);//设置命名空间  
	         List<Element> beans = xsub.selectNodes(doc);//获取文档下所有bean节点
			/** 读命名空间中值# */
			
			
			foo = root.element("req");
			
			System.out.println(foo.elementText("linkid"));
			
			for (Iterator i = root.elementIterator("dync"); i.hasNext();) {
				foo = (Element) i.next();
				System.out.println(foo.elementText("status"));
				System.out.println(foo.elementText("calling"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
